component extends="servicebase" {
	include "../../inc/lib.cfm"; 
	this.entityObj = "article";
	this.entityObjFlat= "articleflat";
	
	public any function list(rc){
		
		var result = {};
		//从对象中进行Hql模式查询
		var offset = (rc.page-1)*rc.rows;
		hql = "from articlebase where " & rc.sqlWhere & rc.sqlSort;
		var result["rows"] = ormExecuteQuery(hql, false, {maxResults=rc.rows, offset=offset});
		var totalhql = "select count(id) as total from articlebase where " & rc.sqlWhere ;
		var result["total"] = ormExecuteQuery(totalhql, true);
		for(i = 1; i <= arraylen(result.rows); i++)
		{
			result.rows[i]["ctotal"] = result.rows[i].getCommentCounts();
		}
		
		//返回数据
		return result;
	}
	
	public any function category(rc){
		
		var result = {};
		var hql = " from categoryflat where categorytypeid=#rc.categorytypeid# order by categoryid asc,seq asc";
		var result["rows"] = ormExecuteQuery(hql,false);
		var totalhql = "select count(id) as total from categoryflat where categorytypeid=#rc.categorytypeid#";
		var result["total"] = ormExecuteQuery(totalhql,true);
		
		newResult = [];
		
		for(var i=1;i<=arraylen(result.rows);i++){
			var tmpResult = {};
			//信息总记录数
			articlebasetotalhql = "select count(id) as total from articlebase where categoryid=#result.rows[i].getId()#";
			tmpResult['rcount'] = ormExecuteQuery(articlebasetotalhql,true); 
			//评论总记录数
			articlecommenttotalhql = "select count(id) as total from articlecomment where categoryid=#result.rows[i].getId()#";
			tmpResult['ccount'] = ormExecuteQuery(articlecommenttotalhql,true); 
			
			tmpResult["text"] = result.rows[i].getText();
			tmpResult["id"] = result.rows[i].getId();
			tmpResult["description"] = result.rows[i].getDescription();
			tmpResult["seq"] = result.rows[i].getSeq();
			tmpResult["_parentId"] = result.rows[i].getCategoryid();
			newResult[i] = tmpResult;
		}
		result["rows"] = newResult;
		
		
		return result;
	}
	
	public any function get(numeric id){
		result = entityLoadByPk("articlebase", arguments.id);
		//获取扩展字段
		articleextend = result.getArticleextend();
		for(var i=1;i<=arraylen(articleextend);i++){
			//将扩展字段的cellvalue值内容赋值到cellname这个变量中去
			result["#articleextend[i].getcellname()#"] = articleextend[i].getcellvalue();
		}
		return result;
	}

	public any function save(any rc){
		transaction {
            //基本信息操作
            var q = entityNew("articlebase");
           	q.setcategory(entityLoadByPk("category", rc.categoryid));
			include "../../inc/saveandupdate.cfm";
			//扩展字段操作
			for(var i=1;i<=listlen(rc.extFields);i++){
				fieldName = listgetat(rc.extFields,i);
				fieldValueString = "rc." & fieldName;
				if( structKeyExists(rc, fieldName)  ){
					filedValue=evaluate(fieldValueString);
					var e = entityNew("articleextend");
					e.setCellname(fieldName);
					e.setCellvalue(filedValue);
					e.setcategoryid(rc.categoryid);
					//添加articlebaseid到articleextend表中
					q.addArticleextend(e);
					entitySave(e);
				}
			}
			transactionCommit();
        }
        return q;
	}
	
	
	//修改操作
	public any function update(any rc){
		transaction {
            var q = entityLoadByPk("articlebase", rc.id);
			include "../../inc/saveandupdate.cfm";
			//扩展字段操作，目前的处理方式是，先将扩展字段中该基本信息所属扩展字段的内容都先全部删除，然后将新值进行重新入表的操作方式
			/*
			TODO
			更合理的方式应该是判断articleextend记录的处理方式，具体流程应该如下
			
			对所有扩展字段进行遍历
				如果(该单一字段的信息内容不为空){
					在扩展表中不存在，则添加
					扩展表中存在，则修改
				}否则{
					扩展表中存在，则删除
				}
			*/
			//删除原有扩展记录
			ORMExecuteQuery("delete from articleextend where  articlebaseid = #rc.id#");
			
			for(var i=1;i<=listlen(rc.extFields);i++){
				fieldName = listgetat(rc.extFields,i);
				fieldValueString = "rc." & fieldName;
				if( structKeyExists(rc, fieldName)  ){
					filedValue=evaluate(fieldValueString);
					var e = entityNew("articleextend");
					e.setCellname(fieldName);
					e.setCellvalue(filedValue);
					e.setcategoryid(rc.categoryid);
					//添加articlebaseid到articleextend表中
					q.addArticleextend(e);
					entitySave(e);
				}
			}
			transactionCommit();
			
        }
        return q;
	}
	
	
	public any function getextendcheckbox(any rc){
		var hql = " from articleextend where articlebaseid=#rc.id# and cellname='#rc.name#'";
		var result= ormExecuteQuery(hql,false);
		return result;
	}
	
	
	public any function test(any rc,any arg){
		/*
		transaction {
		    var tx 		= ORMGetSession().beginTransaction();
			var count   = 0;
			try{
				count = ORMExecuteQuery("delete from articleextend where  articlebaseid = 89");
				tx.commit();
			}
			catch(Any e){
				if(tx.wasCommitted()){ tx.rollback(); }
				throw(e);
			}
			ORMFlush(); 
        }
        */
//        q = entityLoadByPK("articlecomment",1);
          
          super.test(rc,"fuck");
	}
	
	
	//发布操作
	public any function publish(numeric id){
		q = entityLoadByPk("articlebase", arguments.id);
		if( q.getispublish()){v = 0;}else{v = 1;}
		q.setispublish(v);
		entitySave(q);
		transactionCommit();
		return v;
	}
	
	public any function publishbat(rc){
		result = ORMExecuteQuery("update articlebase set ispublish=:ispublish WHERE id in (#rc.ids#)",{ispublish = lsparsenumber(rc.value)});
		return result;
	}
	
	public any function commendbat(rc){
		result = ORMExecuteQuery("update articlebase set iscommend=:iscommend WHERE id in (#rc.ids#)",{iscommend = lsparsenumber(rc.value)});
		return result;
	}
	
	public any function scrollbat(rc){
		result = ORMExecuteQuery("update articlebase set isscroll=:isscroll WHERE id in (#rc.ids#)",{isscroll = lsparsenumber(rc.value)});
		return result;
	}
	
	public any function commentbat(rc){
		result = ORMExecuteQuery("update articlebase set iscomment=:iscomment WHERE id in (#rc.ids#)",{iscomment = lsparsenumber(rc.value)});
		return result;
	}
	
	//推荐操作
	public any function commend(numeric id){
		q = entityLoadByPk("articlebase", arguments.id);
		if( q.getiscommend()){v = 0;}else{v = 1;}
		q.setiscommend(v);
		entitySave(q);
		transactionCommit();
		return v;
	}
	
	//滚动操作
	public any function scroll(numeric id){
		q = entityLoadByPk("articlebase", arguments.id);
		if( q.getisscroll()){v = 0;}else{v = 1;}
		q.setisscroll(v);
		entitySave(q);
		transactionCommit();
		return v;
	}
	
	//评论操作
	public any function comment(numeric id){
		q = entityLoadByPk("articlebase", arguments.id);
		if( q.getiscomment()){v = 0;}else{v = 1;}
		q.setiscomment(v);
		entitySave(q);
		transactionCommit();
		return v;
	}
	
	//删除操作
	public any function delete(numeric id){
		var q = entityLoadByPk("articlebase", arguments.id);
        entityDelete(q);
        transactionCommit();
		return q;
	}
	
}